package com.thinkingstar.iads.common.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.ColType;
import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Comment;
import org.nutz.dao.entity.annotation.EL;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Prev;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public abstract class AbstractEntites extends AbstractPersistentEntity implements Serializable {
	
    private static final long serialVersionUID = 1L;
    
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
    
	@Column("id")
	@Name
	@Comment("主键")
	@ColDefine(type = ColType.VARCHAR, width = 32)
	@Prev(els = { @EL("uuid()") })
	protected String id;
    
    @Column
    @Comment("删除标记")
    @Prev(els = @EL("$me.flag()"))
    @ColDefine(type = ColType.BOOLEAN)
    @ApiModelProperty(value="del_flag",hidden=true)
    protected Boolean del_flag;
    
	@Comment("创建时间")
	@Column
	@ColDefine(type = ColType.VARCHAR, width = 50)
	protected String create_time;
	
	@Comment("修改时间")
	@Column
	@ColDefine(type = ColType.VARCHAR, width = 50)
	protected String update_time;
    
    
    public AbstractEntites() {
    	this.del_flag = flag();
  	}
   
    public String toString() {
        return String.format("/*%s*/%s", super.toString(), Json.toJson(this, JsonFormat.compact()));
    }

    public Boolean flag() {
        return false;
    }

    /**
     * 传入一个集合返回一个Map
     * @param ls
     * @return
     */
    public static <T extends AbstractEntites> Map<String,T> idEntityMap(Collection<T> ls){
    	Map<String,T> map = new HashMap<>();
    	//注意请不要返回一个null 特别是集合不需要
    	if(null == ls || 0 == ls.size()){
    		return map;
    	}
    	for (T entity : ls) {
			map.put(entity.getId(), entity);
		}
    	return map;
    }
    
    
}
